package de.slothsoft.commons.swing.editorview;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import de.slothsoft.commons.res.CommonImages;
import de.slothsoft.commons.res.CommonMessages;
import de.slothsoft.commons.swing.GradientPanel;
import de.slothsoft.commons.swing.JToolBarManager;

/**
 * Has a title bar on the top with the most common buttons
 * 
 * @param <E>
 */

public abstract class AbstractTitleEditor<E> extends AbstractEditor<E> {

	private static final long serialVersionUID = 1L;

	private JLabel errorLabel;

	@Override
	protected void createLayout() {
		Color startColor = createGradientStartColor();
		Color endColor = createGradientEndColor();

		setBackground(endColor);
		setOpaque(false);

		JPanel controlPanel = new JPanel();

		JPanel contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());
		contentPanel.add(createTitleBar(startColor, endColor),
				BorderLayout.NORTH);
		contentPanel.add(controlPanel, BorderLayout.CENTER);

		controlPanel.setBackground(endColor);
		controlPanel.setOpaque(true);
		createMainPanel(controlPanel);

		addContentPanel(contentPanel);
	}

	protected void addContentPanel(JPanel contentPanel) {
		setLayout(new BorderLayout());
		add(contentPanel, BorderLayout.NORTH);
		add(new JLabel(" "), BorderLayout.CENTER);
	}

	protected abstract void createMainPanel(JPanel parent);

	private Component createTitleBar(Color startColor, Color endColor) {
		JPanel toolbarPanel = new GradientPanel(startColor, endColor, false);

		toolbarPanel.setLayout(new BorderLayout());
		toolbarPanel.add(createTitle(), BorderLayout.CENTER);
		toolbarPanel.add(createToolBar(), BorderLayout.EAST);

		return toolbarPanel;
	}

	private Component createTitle() {
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.setOpaque(false);
		panel.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 10));

		JLabel title = new JLabel(getTitle());
		Font oldFont = title.getFont();
		title.setFont(new Font(oldFont.getName(), Font.BOLD,
				oldFont.getSize() + 5));
		panel.add(title, BorderLayout.CENTER);

		this.errorLabel = new JLabel(" ");
		panel.add(this.errorLabel, BorderLayout.SOUTH);
		return panel;
	}

	private Component createToolBar() {
		JToolBarManager manager = new JToolBarManager();
		manager.getToolBar().setFloatable(false);
		manager.getToolBar().setOpaque(false);
		createToolbar(manager);
		makeButtonsPretty(manager.getToolBar());
		return manager.getToolBar();
	}

	private void makeButtonsPretty(JToolBar toolBar) {
		Border border = BorderFactory.createBevelBorder(BevelBorder.RAISED);
		for (Component c : toolBar.getComponents()) {
			if (c instanceof JButton) {
				((JComponent) c).setBorder(border);
			}
		}
	}

	protected void createToolbar(JToolBarManager manager) {
		if (showSaveButton()) {
			manager.add(CommonImages.get(CommonImages.TOOLBAR_SAVE),
					CommonMessages.getString("Common.save"),
					new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent arg0) {
							saveEditor();
						}
					});
		}
	}

	protected boolean showSaveButton() {
		return true;
	}

	public void setErrorMessage(String message) {
		this.errorLabel.setText(message == null ? " " : message);
		this.errorLabel.setIcon(message == null ? null : CommonImages
				.get(CommonImages.STATUS_ERROR_MEDIUM));
	}

	protected Color createGradientStartColor() {
		Color color = new Color(170, 215, 255);
		return color;
	}

	protected Color createGradientEndColor() {
		Color color = UIManager.getColor("Table.background");
		if (color == null) {
			color = Color.WHITE;
		}
		return color;
	}
}
